package com.bit.demo.controller;

import com.bit.demo.model.Person;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@RestController
@Controller
@RequestMapping("/response")
public class ResponseController {
    @RequestMapping("/returnPage")
    public String returnPage() {
        return "/hello.html";
    }
    @ResponseBody
    @RequestMapping("/returnData")
    public String returnDate() {
        return "/hello.html";
    }

    @ResponseBody
    @RequestMapping("/returnHTML")
    public String returnHTML() {
        return "<h1>我是一级标题</h1>";
    }

    @ResponseBody
    @RequestMapping("/returnJSON")
    public List<Person> returnJSON() {
        List<Person> people = new ArrayList<Person>();
        Person person = new Person("haha","df9e9j",31);
        people.add(person);
        return people;
    }
    @ResponseBody
    @RequestMapping("/returnMap")
    public Map<String, String> returnMap() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("name","王小二");
        map.put("age","18");
        map.put("password","13reef");
        return map;
    }
    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response) {
        response.setStatus(400);
        return "设置状态码成功！";
    }

    @ResponseBody
    @RequestMapping("/setHeader")
    public String setHeader(HttpServletResponse response) {
        response.setHeader("myHeader","myHeader");
        return "设置Header成功！";
    }

    @ResponseBody
    @RequestMapping(value = "/setContentType", produces ="application/json")//指定返回内容格式
    public String setContentType(HttpServletResponse response) {

        return "{\"ok\":1}";
    }
}
